package com.cskaoyan.controller.admin.promotion;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.promotion.bo.CouponBo;
import com.cskaoyan.bean.admin.promotion.vo.CouponVo;
import com.cskaoyan.service.admin.PromotionService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name : TopicController.java
 * @Time : 2021/8/12 19:36
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    PromotionService promotionService;

    @RequiresPermissions(value = {"*", "admin:coupon:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo getList(String name, Short type, Short status, BaseParamBo baseParamBo) {
        BaseRespData couponList = promotionService.getCouponList(name, type, status, baseParamBo);
        return BaseRespVo.ok(couponList);
    }

    @RequiresPermissions(value = {"*", "admin:coupon:create"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo createCoupon(@RequestBody CouponBo couponBo) {
        CouponVo coupon = promotionService.createCoupon(couponBo);
        if(coupon == null){
            return BaseRespVo.fail("请正确设置日期（如设置相对天数，需要大于0）");
        }
        return BaseRespVo.ok(coupon);
    }

    @RequiresPermissions(value = {"*", "admin:coupon:read"}, logical = Logical.OR)
    @RequestMapping("read")
    public BaseRespVo readCoupon(Integer id) {
        CouponVo couponVo = promotionService.readCoupon(id);
        return BaseRespVo.ok(couponVo);
    }

    @RequiresPermissions(value = {"*", "admin:coupon:listUser"}, logical = Logical.OR)
    @RequestMapping("listuser")
    public BaseRespVo getListUser(Integer couponId, Integer userId, Short status, BaseParamBo param) {
        BaseRespData couponUser = promotionService.getCouponUser(couponId, userId, status, param);
        return BaseRespVo.ok(couponUser);
    }

    @RequiresPermissions(value = {"*", "admin:coupon:update"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo updateCoupon(@RequestBody CouponBo couponBo) {
        CouponVo couponVo = promotionService.updateCoupon(couponBo);
        if (couponVo == null) {
            return BaseRespVo.fail("没有任何更新操作~");
        }
        return BaseRespVo.ok(couponVo);
    }

    @RequiresPermissions(value = {"*", "admin:coupon:delete"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo deleteCoupon(@RequestBody CouponBo couponBo) {
        promotionService.deleteCoupon(couponBo);
        return BaseRespVo.ok();
    }
}
